package com.mxm.mirai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mxm.mirai.dao.Botconfig;
import com.mxm.mirai.mapper.BotconfigMapper;
import com.mxm.mirai.service.BotconfigService;
import net.mamoe.mirai.Bot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 漫小猫
 * @since 2020-09-24
 */
@Service
public class BotconfigServiceImpl extends ServiceImpl<BotconfigMapper, Botconfig> implements BotconfigService {

    @Autowired
    BotconfigMapper botconfigMapper;

    private Botconfig botconfig=null;
    /**
     *判断qq在BotConfig表中任务是否存在
     * @return
     */
    @Override
    public boolean QueryBotConfigQQIf(String qq, String task) {
        if (0<botconfigMapper.selectCount(new QueryWrapper<Botconfig>().eq("qq",qq).eq("task",task))){
            return true;
        }else {
            return false;
        }
    }
    /**
     *判断bot的任务状态
     * @return
     */
    @Override
    public String QueryBotConfigState(String qq, String task) {
        botconfig = botconfigMapper.selectOne(new QueryWrapper<Botconfig>().select("state").eq("qq",qq).eq("task",task));
        if(botconfig==null){
         return "0";
        }
        return botconfig.getState();
    }
    /**
     *更新qq在BotConfig表中任务,不存在则添加
     * @return
     */
    @Override
    public boolean UpdateBotConfigQQ(String qq, String task, String state, String content) {
        try {
            if (!QueryBotConfigQQIf(qq, task)) {
                botconfig = new Botconfig();
                botconfig.setQq(qq);
                botconfig.setTask(task);
                botconfig.setState(state);
                botconfig.setContent(content);
                botconfigMapper.insert(botconfig);
                return true;
            }
            UpdateWrapper<Botconfig> updateWrapper = new UpdateWrapper<>();
            if (null != state && !"".equals(state) && null != content && !"".equals(content)) {
                updateWrapper.eq("qq", qq).eq("task", task).set("state", state).set("content", content);
            } else if (null != state && !"".equals(state)) {
                updateWrapper.eq("qq", qq).eq("task", task).set("state", state);
            } else if (null != content && !"".equals(content)) {
                updateWrapper.eq("qq", qq).eq("task", task).set("content", content);
            }
            botconfigMapper.update(null, updateWrapper);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
